const app = getApp();
Component({
  data: {
    // selected: -1,
    isIPhoneX:null,
    color: "#111111",
    selectedColor: "#ff7100",
    "list": [{
      "pagePath": "/pages/indexpage/indexpage",
      "iconPath": "/images/f1.png",
      "selectedIconPath": "/images/f1-on.png",
      "text": "首页"
    }, {
      "pagePath": "/pages/communicate_list/communicate_list",
      "iconPath": "/images/f6.png",
      "selectedIconPath": "/images/f6-on.png",
      "text": "沟通",
      "megNum":0
    }, {
      "pagePath": "/pages/seek_job/seek_job",
      "iconPath": "/images/f3.png",
      "selectedIconPath": "/images/f3-on.png",
      "text": "找工作"
    },
    {
      "pagePath": "/pages/interview_invitation/interview_invitation",
      "iconPath": "/images/f4.png",
      "selectedIconPath": "/images/f4-on.png",
      "text": "面试邀请"
    },
      {
      "pagePath": "/pages/user_center/user_center",
      "iconPath": "/images/f5.png",
      "selectedIconPath": "/images/f5-on.png",
      "text": "我的"
    }]
  },
  attached() {
    this.updateItem();
    app.eventBus.on(app.Events.UserLogin, this.eventOnUserLogin, this);
    app.eventBus.on(app.Events.UserLogout, this.eventOnUserLogout, this);
    app.eventBus.on(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate, this);

    let isIPhoneX = this.data.isIPhoneX;
    return new Promise((resolve, reject) => {
      if (isIPhoneX !== null) {
        resolve(isIPhoneX);
      }
      else {
        wx.getSystemInfo({
          success: ({ model, screenHeight }) => {
            const iphoneX = /iphone x/i.test(model);
            const iphoneNew = /iPhone11/i.test(model);
            isIPhoneX = iphoneX || iphoneNew;
            resolve(isIPhoneX);
            this.setData({
              isIPhoneX: isIPhoneX
            });
          },
          fail: reject
        });
      }
    });

  },
  detached() {
    app.eventBus.off(app.Events.UserLogin, this.eventOnUserLogin);
    app.eventBus.off(app.Events.UserLogout, this.eventOnUserLogout);
    app.eventBus.off(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate);
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const url = data.path;
      if(data.index===3 || data.index===4) {
        if(!getApp().getUserinfo()) {
          wx.navigateTo({url: '/pages/change/change'});
          return;
        }
      }
      if(data.index===2 && !url) {
        wx.navigateTo({url:'/pages/looking_for_talent/looking_for_talent'});
        return;
      }
      if(data.index===3 && !url) {
        wx.navigateTo({url:'/pages/invitation/invitation'});
        return;
      }
      wx.switchTab({url:url});
      // 注释下面, 避免tab切换 active状态闪现
      // this.setData({
      //   selected: data.index
      // });
    },
    setTabBarItem(obj) {
      let index = obj.index;
      delete obj['index'];
      this.data.list[index] = obj;
      this.setData({list: this.data.list});
    },
    updateItem() {
      let userinfo = getApp().getUserinfo();

      if(userinfo && userinfo.group_id!=1) {
        this.setTabBarItem({
          index: 2,
          'text': '找人才',
          "iconPath": "/images/f3.png",
          "selectedIconPath": "/images/f3-on.png",
        });
        this.setTabBarItem({
          index: 3,
          'text': '面试邀请',
          "iconPath": "/images/f4.png",
          "selectedIconPath": "/images/f4-on.png",
        });
      }else {
        this.setTabBarItem({
          index: 2,
          'text': '找工作',
          "iconPath": "/images/f3.png",
          "selectedIconPath": "/images/f3-on.png",
          "pagePath": "/pages/seek_job/seek_job"
        })
        this.setTabBarItem({
          index: 3,
          'text': '面试邀请',
          "iconPath": "/images/f4.png",
          "selectedIconPath": "/images/f4-on.png",
          "pagePath": "/pages/interview_invitation/interview_invitation"
        })
      }
    },
    eventOnUserLogin() {
      this.updateItem();
    },
    eventOnUserLogout() {
      this.updateItem();
    },
    eventOnUserinfoUpdate() {
      this.updateItem();
    }
  }
});
